package org.example.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.school.entity.Parents;
import org.example.school.entity.Users;
import org.example.school.mapper.ParentsMapper;
import org.example.school.mapper.UsersMapper;
import org.example.school.model.dto.GuardianProfileDTO;
import org.example.school.service.IParentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 家长/监护人信息表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-09-28
 */
@Service
public class ParentsServiceImpl extends ServiceImpl<ParentsMapper, Parents> implements IParentsService {
    @Autowired
    private ParentsMapper parentsMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public GuardianProfileDTO getGuardianProfile(Long userId) {
        // 1. 获取监护人基本信息
        Parents parent = this.getOne(new LambdaQueryWrapper<Parents>().eq(Parents::getUserId, userId));
        if (parent == null) return null;

        // 2. 获取用户通用信息
        Users user = usersMapper.selectById(userId);

        // 3. 组装DTO
        GuardianProfileDTO dto = new GuardianProfileDTO();
        dto.setFullName(parent.getFullName());
        if (user != null) {
            dto.setPhoneNumber(user.getPhoneNumber());
        }

        // 4. 查询并设置孩子信息
        List<GuardianProfileDTO.ChildInfo> children = parentsMapper.findChildrenInfoByParentId(parent.getId());
        dto.setChildren(children);

        return dto;
    }

}
